package quiz;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

public class Quiz {
	
	/* ================ STATIC METHODS AND VARIABLES ================ */

	/**
	 * Function sets the DataBase object that will be referenced by all answers
	 * MUST BE CALLED BEFORE ANY ANSWERS ARE CREATED
	 * @param db - the DataBase object referenced by all accounts 
	 */
	public static void setDataBase(DataBase database){
		db = database;
		db.open();
	}
	public static DataBase db;
	
	
	/* ================ STATIC METHODS AND VARIABLES ================ */
	
	/* ================ PRIVATE INSTANCE VARIABLES ================ */


	private int id;
	private String name;
	private String description;
	private boolean is_ordered;
	private boolean is_singlePage;
	private boolean has_practiceMode;
	private boolean immediate_correction;
	private boolean visible_to_public;
	private Account creator;
	private Date date_created;
	
	/* ================ PRIVATE INSTANCE VARIABLES ================ */
	
	public Quiz(String name, String description, boolean is_ordered, boolean is_singlePage, boolean has_practiceMode, boolean immediate_correction, boolean visible_to_public, int creatorID) {
		this.name = name;
		this.description = description;
		this.is_ordered = is_ordered;
		this.is_singlePage = is_singlePage;
		this.has_practiceMode = has_practiceMode;
		this.immediate_correction = immediate_correction;
		this.visible_to_public = visible_to_public;
		this.creator = db.getAccount(creatorID);
		this.date_created = new Date();
	}
	
	public Quiz(String name, String description, boolean is_ordered, boolean is_singlePage, boolean has_practiceMode, boolean immediate_correction, boolean visible_to_public, Account creator) {
		this.name = name;
		this.description = description;
		this.is_ordered = is_ordered;
		this.is_singlePage = is_singlePage;
		this.has_practiceMode = has_practiceMode;
		this.immediate_correction = immediate_correction;
		this.visible_to_public = visible_to_public;
		this.creator = creator;
		this.date_created = new Date();
	}
	
	/* ================ GETTERS ================ */


	public int getID() {
		return id;
	}
	
	public String getDescription() {
		return description;
	}
	
	public String getName() {
		return name;
	}

	public boolean is_ordered() {
		return is_ordered;
	}

	public boolean is_singlePage() {
		return is_singlePage;
	}

	public boolean has_practiceMode() {
		return has_practiceMode;
	}

	public boolean should_correctImmediately() {
		return immediate_correction;
	}

	public boolean isVisible_to_public() {
		return visible_to_public;
	}

	public Account getCreator() {
		return creator;
	}

	public Date getDate_created() {
		return date_created;
	}
	
	public ArrayList<Question> getQuestions() {
		ArrayList<Question> questions = db.getQuizQuestions(this.id);
		if(!is_ordered) Collections.shuffle(questions);
		
		return questions;
	}
	
	public int getTotalPossibleScore() {
		ArrayList<Question> questions = db.getQuizQuestions(this.id);
		int total = 0;
		
		for(Question question : questions) total += question.getWeight();
		
		return total;
	}
	
	public ArrayList<String> getTags() {
		return db.getTags(this.id);
	}
	
	public ArrayList<Result> getHighestResults(int num) {
		return db.getTopQuizResults(this.id, num);
	}
	
	public ArrayList<Result> getHighestResultsDay(int num) {
		return db.getTopQuizResultsInDay(this.id, num);
	}
	
	public ArrayList<Result> getRecentResults(int num) {
		return db.getRecentResultsForQuiz(this.id, num);
	}
	
	public Result getAverageResult() {
		return db.getAverageResultForQuiz(this.id);
	}
	

	/* ================ GETTERS ================ */
	
	/* ================ SETTERS ================ */
	
	
	public void setID(int num) {
		this.id = num;
	}

	public void setName(String name) {
		this.name = name;
		db.editQuiz(this);
	}

	public void setIs_ordered(boolean is_ordered) {
		this.is_ordered = is_ordered;
		db.editQuiz(this);
	}

	public void setIs_singlePage(boolean is_singlePage) {
		this.is_singlePage = is_singlePage;
		db.editQuiz(this);
	}

	public void setHas_practiceMode(boolean has_practiceMode) {
		this.has_practiceMode = has_practiceMode;
		db.editQuiz(this);
	}

	public void setImmediate_correction(boolean immediate_correction) {
		this.immediate_correction = immediate_correction;
		db.editQuiz(this);
	}

	public void setVisible_to_public(boolean visible_to_public) {
		this.visible_to_public = visible_to_public;
		db.editQuiz(this);
	}

	public void setCreator(int creatorID) {
		this.creator = db.getAccount(creatorID);
		db.editQuiz(this);
	}
	
	public void setCreator(Account creator) {
		this.creator = creator;
		db.editQuiz(this);
	}

	public void setDate_created(Date date_created) {
		this.date_created = date_created;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public void setTag(String tag) {
		db.storeTag(this.id, tag.toLowerCase().trim());
	}
	
	public void deleteTag(String tag) {
		db.deleteTag(this.id, tag.toLowerCase().trim());
	}
	
	/* ================ SETTERS ================ */

	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + creator.getID();
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Quiz other = (Quiz) obj;
		if (creator.getID() != other.creator.getID())
			return false;
		if (id != other.id)
			return false;
		return true;
	}

}
